/*
 * @Author: c
 * @Date: 2023-12-27 11:37:00
 * @LastEditTime: 2024-01-20 00:16:43
 * @LastEditors: c
 * @Description: 
 * @FilePath: \music-web-backend\src\common\const\constants.ts
 * https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// Copyright 2023 Administrator
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { MulterOptions } from "@nestjs/platform-express/multer/interfaces/multer-options.interface";
import { diskStorage } from "multer";
import { extname, join } from "path";

export const SysConstants = {

    /**
     * 根菜单节点ID
     */
    ROOT_NODE_ID: 0,

    /**
     * 根菜单节点ID
     */
    ROOT_CATE_ID: 0,


    /**
     * 系统默认密码
     */
    DEFAULT_PASSWORD: "123456",

    /**
     * 超级管理员角色编码
     */
    ROOT_ROLE_CODE: "ROOT",

    /**
     * 超级管理员用户名
     */
    ROOT_USER_NAME: "root"


}
type P = keyof typeof EXCEPTION
type Keys = 'USER' | 'COMMON' | '003'

export interface IEXCEPTIONMSG {
    CODE: string, DESC: string
}

export interface IEXCEPTIONType {
    NOT_FOUND: IEXCEPTIONMSG
}

interface IEXCEPTION {
    USER: IEXCEPTIONType;
    EXIST: IEXCEPTIONMSG;
    COMMON: Record<string, IEXCEPTIONMSG | string>;
}

export const EXCEPTION = {
    USER: {
        // 用户不存在
        NOT_FOUND: {
            CODE: 'A1000',
            DESC: '用户不存在',
        },


        // 用户已经存在
        EXIST: {
            CODE: 'A1001',
            DESC: '用户已存在',
        },


    },



    // 通用
    COMMON: {
        CODE: 'A0000',
        DESC: '操作失败',
    },


    // 通用
    EXIST: {
        CODE: 'A0001',
        DESC: '数据不能重复',
    },
}



// 配置文件上传
export const multerOptions: MulterOptions = {
    // 配置文件的存储
    storage: diskStorage({
        // 存储地址
        destination: join(__dirname, '../images'),
        // 存储名称
        filename: (_req, file, callback) => {
            const suffix = extname(file.originalname); // 获取文件后缀
            const docName = new Date().getTime(); // 自定义文件名
            return callback(null, `${docName}${suffix}`);
        },
    }),
    // 过滤存储的文件
    fileFilter: (_req, file, callback) => {
        // multer 默认使用 latin1 编码来解析文件名, 而 latin1 编码不支持中文字符, 所以会出现中文名乱码的现象
        // 这里将文件名从 latin1 编码转换为 Buffer 对象, 再用 toString('utf8') 将 Buffer 对象转换为 utf8 编码的字符串
        // utf8 是一种支持多国语言的编码方式, 这样就可以保证文件名的中文字符不会被错误解析
        file.originalname = Buffer.from(file.originalname, 'latin1').toString(
            'utf8',
        );
        callback(null, true);
    },
    // 限制文件大小
    limits: {
        // 限制文件大小为 10 MB
        fileSize: 10 * 1024 * 1024, // 默认无限制
        // 限制文件名长度为 50 bytes
        fieldNameSize: 50, // 默认 100 bytes
    },
};
